<?php
/**
*
* 功能描述（订单数据）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class ModelOrder extends ModelBase
{
	function Create($data)
	{
		$id = $this->db->Insert("order_base", $data);
		return $id;
	}

	function SaveData($id, $cart)
	{
		$sql = "insert into order_goods(`order_id`, `good_id`, `name`, `version`, `size`, `price`, `cnt`, `discount`) values";
		foreach($cart as $item)
		{
			if($item['status'] == 0)
				$sql .= "($id, " . $item['id'] . ", '" . $item['name'] . "', " . $item['version']
					. ", " . $item['size'] . ", " .$item['price'] . ", " . $item['cnt'] . ", " . $item['discount'] ."),";
		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$id = $this->db->execute($sql);
		return $id;
	}

	function GetList($userid, $page)
	{
		$where = "userid = $userid";
		$data  = $this->db->getRow("order_base", 'count(1) cnt', $where);
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());
		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / ORDER_PAGESIZE));
		$limit = ORDER_PAGESIZE * ($page - 1) . ", " . ORDER_PAGESIZE;
		$data = $this->db->GetAll("order_base", "id, status, createtime, `pay_type`, pay_adm != 0 pay_status, amount", $where, 'id desc', $limit);
		$retData['data'] = $data;
		return $retData;
	}

	function GetGoods($orderid)
	{
		$data = $this->db->GetAll("order_goods", "`id`, `good_id`, `name`, `version`, `size`, round(`price`*discount, 2) price, `cnt`, `comment_id`", "order_id = $orderid");
		return $data;
	}

	function GetGoodBase($userid, $id)
	{
		$data = $this->db->GetRow("order_goods", "`order_id`, `good_id`, `version`, `size`", "id = $id and comment_id = 0");
		if(!$data)
			return false;
		//校验合法性
		$oid = $data['order_id'];
		$data2 = $this->db->GetRow("order_base", "userid", "id = $oid and userid = $userid");
		if(!$data2)
			return false;
		return $data;
	}

	function SetCommentId($id, $comId)
	{
		$cnt = $this->db->Update("order_goods", array('comment_id' => $comId), "id = $id");
		return $cnt;
	}

	function Update($data, $oid, $uid, $where='')
	{
		return $this->db->Update('order_base', $data, "id = $oid and userid = $uid $where");
	}

	function GetBaseInfo($orderId, $userid)
	{
		$filds = "`id`, `status`, `pay_type`, `trade_money1`, `trade_money2`, `amount_pre`, `amount`, `message`";
		$data = $this->db->GetRow("order_base", $filds, "id = $orderId and userid = $userid and status = 1 and pay_adm  = 0");
		return $data;
	}

	function GetDetail($orderId, $userid)
	{
		$filds = "`id`, `status`, `pay_type`, pay_adm != 0 pay_status, `trade_money1`, `trade_money2`, `amount_pre`, `amount`, `card1`, `card2`, `attach1`, `attach2`, `area`, `address`, `consignee`, `mobile_to`, `mobile_from`, `sign_building`, `delivery_type`, `delivery_time`, `inv_type`, `inv_title`, `message`";
		$data = $this->db->GetRow("order_base", $filds, "id = $orderId and userid = $userid");
		return $data;
	}
}

